package org.tgcloud.group.core.ExportUtils.test;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSONObject;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFPictureData;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @className: ExcelUtil
 * @description: [描述说明该类的功能]
 * @author: [姓名]
 * @version: [v1.0]
 * @createTime: [2023/9/20 15:50]
 * @updateUser: [姓名]
 * @updateTime: [2023/9/20 15:50]
 * @updateRemark: [描述说明本次修改内容]
 */
public class ExcelUtil {
    private List<ExcelSheet> sheetList=new ArrayList<>();
    private String rootPath = "";
    private String imageTargetPath = "";
    private Workbook workbook = null;

    public ExcelUtil(String filePath,String imageTargetPath ){
        this.rootPath=filePath;
        this.imageTargetPath=imageTargetPath;
        readExcel(filePath);
    }

    private void readExcel(String path) {
        try {
            this.rootPath = path;
            // 获取文件输入流
            InputStream inputStream = new FileInputStream(path);
            // 定义一个org.apache.poi.ss.usermodel.Workbook的变量
            Workbook workbookModel = null;
            // 截取路径名 . 后面的后缀名，判断是xls还是xlsx
            String fileType = path.substring(path.lastIndexOf(".") + 1);

            if (fileType.equals("xls")) {
                workbookModel = new HSSFWorkbook(inputStream);
            } else if (fileType.equals("xlsx")) {
                workbookModel = new XSSFWorkbook(inputStream);
            }
            this.workbook = workbookModel;
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public ExcelSheet getSheet(int index,Integer titleRow){
        Sheet sheet = workbook.getSheetAt(index);
        return new ExcelSheet(sheet,titleRow,rootPath,imageTargetPath);
    }

    public static void main(String[] args) {
//        String path = "D:\\图片壁纸\\茶里-重庆邓倩-10.9(3)(3)(1).xls";
        String path = "D:\\图片壁纸\\test1.xlsx";
        String imageTargetPath = "D:\\upload";

        ExcelUtil excelUtil = new ExcelUtil(path, imageTargetPath);
        ExcelSheet sheet = excelUtil.getSheet(0,1);


        System.out.println(sheet.getRowDataList());
        List<Map<Integer, ExcelDataEntry>> rowDataList = sheet.getRowDataList();
        String s = JSONObject.toJSONString(rowDataList);
        System.out.println(s);


        List<ExcelDataEntry> a = sheet.getDataByTitle("编号" );
        System.out.println(a);

        ExcelDataEntry c = sheet.getDataByTitle("编号" ,2);
        System.out.println(c);

        ExcelDataEntry d = sheet.getDataByTitle(1,"编号" ,2);
        System.out.println(d);

        List<Map<Object, ExcelDataEntry>> colDataList = sheet.getColDataList();
        String colDataListString = JSONObject.toJSONString(colDataList);
        System.out.println(colDataListString);

    }


}